package daiwei.javaWeb.exam.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import daiwei.javaWeb.exam.meta.Product;

public class IdCheck implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		boolean flag = true;
		String id = request.getParameter("id");
		HttpSession session = request.getSession();
		@SuppressWarnings("unchecked")
		List<Product> products=(List<Product>) session.getAttribute("productList");
		if( id != null&&products!=null) {
			if(products.get(products.size()-1).getId()<Integer.parseInt(id)) {
				if(session.getAttribute("flag")==null) {
					response.sendRedirect("/");
					flag = false;
				}
			}
		} else {
			System.out.println(2);
			response.sendRedirect("/");
			flag = false;
		}
		return flag;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
